﻿using Microsoft.EntityFrameworkCore;
using System;

namespace Batman.Maintenance.Repositories
{
    public class DbContextFactory<TDbContext> where TDbContext : DbContext
    {
        IServiceProvider _serviceProvider;

        /// <summary>
        /// TDbContext应当已被注册到ServiceCollection中
        /// </summary>
        /// <param name="serviceProvider"></param>
        public DbContextFactory(IServiceProvider serviceProvider)
        {
            _serviceProvider = serviceProvider;
        }

        public DbContext GetDbContext()
        {
            return _serviceProvider.GetService(typeof(TDbContext)) as DbContext;
        }
    }
}
